package com.github;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/10 下午3:52
 */
public class Test40 {
    public static void main(String[] args) throws InterruptedException {

        AtomicInteger ato = new AtomicInteger(0);

        CountDownLatch la = new CountDownLatch(10);

        for(int i = 0;i < 10;i++){
            new Thread(() ->{
                for(int j = 0;j < 1000;j++) {
                    ato.getAndIncrement();
                }
                la.countDown();
            }).start();
        }

        la.await();
        System.out.println(ato.get());


        //ExecutorService pool = Executors.newCachedThreadPool();


        //ThreadPoolExecutor pool = new ThreadPoolExecutor(0, 2,
        //        60L, TimeUnit.SECONDS,
        //        new SynchronousQueue<>());
        //
        //for(int i = 0;i < 3;i++){
        //    pool.execute(() ->{
        //        System.out.println(Thread.currentThread() + "start");
        //
        //        try {
        //            Thread.sleep(60000);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        //
        //        System.out.println(Thread.currentThread() + "end");
        //    });
        //}

        //SynchronousQueue<Integer> queue = new SynchronousQueue<>();
        //
        //new Thread(() ->{
        //
        //    try {
        //        Thread.sleep(3000);
        //    } catch (InterruptedException e) {
        //        e.printStackTrace();
        //    }
        //
        //    System.out.println(queue.size());
        //
        //    boolean offer = queue.offer(333);
        //
        //    System.out.println(offer);
        //
        //    System.out.println(queue.size());
        //    //try {
        //    //    queue.put(333);
        //    //} catch (InterruptedException e) {
        //    //    e.printStackTrace();
        //    //}
        //}).start();
        //
        //System.out.println("1");

        //queue.take();
        //
        //System.out.println("2");


        //
        //new Thread(() ->{
        //
        //    try {
        //
        //        System.out.println(queue.size());
        //
        //        Integer t = queue.take();
        //
        //        System.out.println(queue.size());
        //        System.out.println(t);
        //    } catch (InterruptedException e) {
        //        e.printStackTrace();
        //    }
        //}).start();
        //
        //queue.put(333);
        //
        //System.out.println("end");
    }

    private static int test() throws InterruptedException {
        Map<String, Integer> map = new HashMap<String, Integer>();
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 8; i++) {
            pool.execute(new MyTask(map));
        }
        pool.shutdown();
        pool.awaitTermination(1, TimeUnit.DAYS);

        return map.get(MyTask.KEY);
    }
}

class MyTask implements Runnable {

    public static Object lock = new Object();

    public static final String KEY = "key";

    private Map<String, Integer> map;

    public MyTask(Map<String, Integer> map) {
        this.map = map;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (lock) {
                this.addup();
            }
        }
    }

    private void addup() {
        if (!map.containsKey(KEY)) {
            map.put(KEY, 1);
        } else {
            map.put(KEY, map.get(KEY) + 1);
        }
    }
}
